package com.controller.system;


import com.common.ResultUtil;
import com.common.WebContextHelper;
import com.common.stereotype.Uri;
import com.controller.base.BaseLocalController;
import com.entity.Manager;
import com.entity.Menu;
import com.facade.menu.IMenuFacadeCUD;
import com.facade.menu.IMenuFacadeR;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
@RequestMapping("/menu")
//@Module(parent = "系统模块", name = "菜单管理", parentModuleOrder = 0, childModuleOrder = 1)
public class MenuController extends BaseLocalController {

    @Resource
    private IMenuFacadeR menuFacadeR;
    @Resource
    private IMenuFacadeCUD menuFacadeCUD;

    @ModelAttribute
    public void prepare(Long id, ModelMap modelMap) {
        HttpServletRequest request = WebContextHelper.getRequest();
        String requestUri = request.getRequestURI();
        if (requestUri.contains("modify") && null != id) {
            modelMap.addAttribute(menuFacadeR.findByPrimaryKey(id));
        }
    }

    /**
     * 用户菜单加载
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/dataMenu.htm")
    public ResultUtil dataMenu() {
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return ResultUtil.newInstance().setData(menuFacadeR.findRankByUserLink(onLineUser.getId(), "tMenu.seq ASC"));
    }

    @RequestMapping(value = "/query.htm")
//    @Uri(name = "查询", uri = "/menu/query", order = 0)
    public String query(ModelMap modelMap) {
        modelMap.addAttribute("menuList", menuFacadeR.findRankAll("seq ASC"));
        return "system/menu-List";
    }

    @RequestMapping(value = "/edit.htm")
    public String edit(Long id, ModelMap modelMap) {
        modelMap.addAttribute("menuListLv1", menuFacadeR.findByPropertyEqualTo("pid", null, "seq ASC"));
        if (null != id) {
            Menu menu = menuFacadeR.findByPrimaryKey(id);
            if (null != menu) {
                modelMap.addAttribute("menu", menu);

                String fullIds = menu.getFullIds();
                if (StringUtils.isNotBlank(fullIds)) {
                    String[] pidArray = fullIds.split(",");
                    String menuIdLv1 = pidArray[0];
                    modelMap.addAttribute("menuIdLv1", menuIdLv1);
                    modelMap.addAttribute("menuListLv2", menuFacadeR.findByPropertyEqualTo("pid", menuIdLv1, "seq ASC"));
                    if (pidArray.length == 2) {
                        modelMap.addAttribute("menuIdLv2", pidArray[1]);
                    }
                }
            }
        }
        return "system/menu-edit";
    }

//    @Token
    @ResponseBody
    @RequestMapping(value = "/add.htm", method = RequestMethod.POST)
    @Uri(name = "添加", uri = "/menu/add", order = 1)
    public ResultUtil add(Menu menu, BindingResult error) {
        ResultUtil resultUtil = validate(error);
        if (ResultUtil.SUCCESS != resultUtil.getStatus()) {
            return resultUtil;
        }
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return menuFacadeCUD.addMenu(menu, onLineUser.getId());
    }

//    @Token
    @ResponseBody
    @RequestMapping(value = "/modify.htm", method = RequestMethod.POST)
//    @Uri(name = "修改", uri = "/menu/modify", order = 2)
    public ResultUtil modify(Menu menu, BindingResult error) {
        ResultUtil resultUtil = validate(error);
        if (ResultUtil.SUCCESS != resultUtil.getStatus()) {
            return resultUtil;
        }
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return menuFacadeCUD.modifyMenu(menu, onLineUser.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/delete.htm/{id}")
//    @Uri(name = "删除", uri = "/menu/delete", order = 3)
    public ResultUtil delete(@PathVariable("id") Long id) {
        Manager onLineUser = WebContextHelper.getOnLineUser();
        return menuFacadeCUD.deleteMenu(id, onLineUser.getId());
    }

}